<?php

// +----------------------------------------------------------------------
// | 分类控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\app\admin;

use yicmf\builder\Edit;
use yicmf\builder\Table;
use think\facade\Url;
use app\controller\admin\Common;
use app\model\index\CreditcardOrder as CreditcardOrderModel;
use app\model\app\Task as TaskModel;


class CreditcardOrder extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
    }

    public function index()
    {

        $statusOptions = [
            [
                'id' => 1,
                'value' => '信用卡'
            ],
            [
                'id' => 2,
                'value' => '贷款'
            ],
            [
                'id' => 3,
                'value' => '租机'
            ]
        ];
        //读取规则列表
        $map = $this->_search($this->dbname);
        //            //显示页面
        $builder = new Table();
        $builder->model(CreditcardOrderModel::class)
            ->where($map)
            ->order('id DESC')
            ->setSearchPostUrl('', '')
            ->searchTextLike('bankName', '产品名称：')
//            ->searchSelect('type','产品类型',$statusOptions)
//            ->searchBool('is_new','新品')
//            ->searchBool('is_hot','推荐')
            ->buttonUpdate('update')
            ->keyId()
            ->keyText('productName', '产品名称')
//                ->keyText('label1', '标签1')
//                ->keyText('label2', '标签2')
            ->keyMap('type', '产品类型', ['', '信用卡', '贷款', '租机'])
            ->keyText('amount', '佣金(额度)')
            ->keyText('userName','提交用户名')
            ->keyText('userTel','提交用户电话')
            ->keyUser('user_id','推广用户')
//                ->keyText('applyMsg','简介')
//            ->keyText('sort', '排序')
            ->keyBool('status', '状态')
//            ->quickUpdate(['bankName','successNum','applyNum','commission'
////                    ,'type' =>['option' => ['1'=>'你好'], 'type' => 'select']
//                ,'is_hot' =>['option' => ['type'=>'text'], 'type' => 'switch']
//                ,'is_new' =>['option' => ['type'=>'text'], 'type' => 'switch']
//                ,'status' =>['option' => ['type'=>'text'], 'type' => 'switch']
//            ])
            ->keyCreateTime()
            ->keyUpdateTime()
            ->actionUpdate()->actionDelete();
        return $builder->fetch();
    }

    /**
     * 编辑分类
     * @access protected
     * @author 微尘 <yicmf@qq.com>
     */
    public function update()
    {
        if ($this->request->isPost()) { //提交表单
            $check = $this->validate($this->request->param(),\app\validate\app\CreditcardOrder::class);
            if ($check !== true)
            {

                $this->_returnError($check);
            }else{
                if ($this->request->param('id')) {

                    $data = CreditcardOrderModel::where('id', $this->request->param('id'))->find();
                    $info = $this->request->except(['id']);
                    $data->save($info);
                    if ($task = TaskModel::where('type',6)->where('relation_id',$data['id'])->find()) {
                        // 更新
                        $task['title'] = $data['bankName'];
                        $task['commission'] = $data['commission'];
                        $task['introduce'] = $data['shortIntroduce'];
                        $task->save();
                    }else{
                        $task['name'] = '信用产品';
                        $task['title'] = $data['bankName'];
                        $task['commission'] = $data['commission'];
                        $task['is_top'] = $data['is_hot'];
                        $task['introduce'] = $data['shortIntroduce'];
                        $task['relation_id'] = $data['id'];
                        $task['user_id'] = $this->user['id'];
                        $task['type'] = 6;
                        $re = TaskModel::create($task);
                    }
                    $this->_returnSuccess('更新成功');
                } else {
                    $re = CreditcardOrderModel::create($this->request->param());
                    if ($re) {
                        // 新增任务
                        $task['name'] = '信用产品';
                        $task['title'] = $re['bankName'];
                        $task['introduce'] = $re['shortIntroduce'];
                        $task['commission'] = $re['commission'];
                        $task['is_top'] = $re['is_hot'];
                        $task['relation_id'] = $re['id'];
                        $task['user_id'] = $this->user['id'];
                        $task['type'] = 6;
                        $re = TaskModel::create($task);
                        if ($re) {
                            $this->_returnSuccess('新增成功');
                        } else {
                            $this->_returnError('新增异常');
                        }
                    } else {

                        $this->_returnError('新增异常');
                    }
                }
            }

        } else {
            /* 获取分类信息 */
            $id = $this->request->get('id');
            $isEdit = $id ? true : false;
            if ($isEdit) {
                $data = CreditcardOrderModel::where('id', $id)->find();
                if (!$data) {
                    $this->_returnError('分类不存在或者已经删除');
                }
            } else {
                $data['type'] = 1;
            }
            $builder = new Edit();
            $builder->title('编辑')
                ->keyText('bankName', '产品名称')
                ->keyText('product_url', '渠道链接')
                ->keyImage('logo', 'logo', '')
                ->keyImage('poster_image', '推广海报', '');
//                ->keyImageModel('poster_image_id', '推广海报', '');
            if (!$isEdit) {
                $builder->keyRadio('type', '产品类型', [1 => '信用卡', 2 => '贷款', 3 => '租机'], '', 1);
            }

            $builder->keyRadio('commission_type', '佣金结算方式', ['0' => '佣金结算', '1' => '百分比结算', '2' => '阶段结算'], '', 0)
                ->keyRadio('settlementType', '结算方式', ['1' => '自动结算', '2' => '手动结算'], '', 1)
                ->keyText('commission_remark', '阶段结算备注')
                ->keyText('label1', '标签1')
                ->keyText('label2', '标签2')
                ->keyDecimal('commission', '佣金')
                ->keySort()
                ->keyNumber('virtualNum', '虚拟下卡数量')
                ->keyTextArea('shortIntroduce', '产品简介')
                ->keyEditor('settlementRules', '结算规则')
                ->keyEditor('applyMsg', '申请条件');
            if ($isEdit) {
                if ($data['type'] == 1)
                {
                    $builder->keyEditor('interestMsg', '权益介绍');
                }else{
                    $builder->keyEditor('interestMsg', '注意事项');
                }
            }else{
                $builder->keyEditor('interestMsg', '权益介绍[注意事项]');
            }
            $builder->keyBool('is_new', '新品')
                ->keyBool('is_hot', '推荐')
                ->keyBool('status', '是否上架')
                ->data($data)
                ->buttonSubmit('', '保存');
            //                    ->buttonClose();
            return $builder->fetch();
        }
    }
}